package com.xxx.sql;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;


public class jdbcDome03 {
    //jdbc4版本
    public static void main(String[] args) {
        query();//查询
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("张三ss", 36, 170.36, "男", new Date(System.currentTimeMillis()), 2));
        list.add(new Student("李四ss", 26, 176.36, "男", new Date(System.currentTimeMillis()), 2));
        insert(list);//插入
        delete();//删除
        update();//修改

    }

    public static void update() {
        String sql = "UPDATE stu SET name = '李四sss',age = 33 WHERE id = 15";
        try (Connection connection = getConnection()
        ) {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    public static void delete() {
        String sql = "delete from stu where id = 7";
        try (Connection connection = getConnection()
        ) {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static Connection getConnections() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/lpp", "root", "123456");
    }

    public static void insert(ArrayList<Student> list) {
        String sql = "insert  into stu(name , age, height,sex,birthday,cla_id) values (?,?,?,?,?,?)";
        try (Connection connection = getConnection()
        ) {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            Iterator<Student> iterator = list.iterator();
            while (iterator.hasNext()) {
                Student next = iterator.next();
                preparedStatement.setString(1, next.getName());
                preparedStatement.setInt(2, next.getAge());
                preparedStatement.setDouble(3, next.getHeight());
                preparedStatement.setString(4, next.getSex());
                preparedStatement.setDate(5, next.getBirthday());
                preparedStatement.setInt(6, next.getClaId());
                int i = preparedStatement.executeUpdate();
                System.out.println(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/lpp", "root", "123456");
    }


    private static void query() {
        try (Connection connection = getConnection();
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("select * from stu")) {
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                double height = resultSet.getDouble("height");
                String sex = resultSet.getString("sex");
                Date birthday = resultSet.getDate("birthday");
                int claId = resultSet.getInt("cla_id");

                Student student = new Student(name, age, height, sex, birthday, claId);
                list.add(student);
            }
            list.stream().forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}